package cn.wjx.taobaounion.presenter.impl;

import cn.wjx.taobaounion.model.Api;
import cn.wjx.taobaounion.model.domain.SelectedCategories;
import cn.wjx.taobaounion.model.domain.SelectedContent;
import cn.wjx.taobaounion.presenter.ISelectedPresenter;
import cn.wjx.taobaounion.utils.LogUtils;
import cn.wjx.taobaounion.utils.RetrofitManager;
import cn.wjx.taobaounion.view.ISelectedPageCallback;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static java.net.HttpURLConnection.HTTP_OK;

/**
 * @author WuChangJian
 * @date 2020/6/25 14:21
 */
public class SelectedPresenter implements ISelectedPresenter {

    private ISelectedPageCallback mViewCallback;
    private Api mApi;

    public SelectedPresenter() {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getCategories() {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }
        Call<SelectedCategories> task = mApi.getSelectedCategories();
        task.enqueue(new Callback<SelectedCategories>() {
            @Override
            public void onResponse(Call<SelectedCategories> call, Response<SelectedCategories> response) {
                int code = response.code();
                SelectedCategories categories = response.body();
                if (code == HTTP_OK) {
                    if (mViewCallback != null) {
                        mViewCallback.onCategoriesLoaded(categories);
                    }
                } else {
                    LogUtils.d(SelectedPresenter.this, "request code-->" + code);
                    onLoadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedCategories> call, Throwable t) {
                LogUtils.d(SelectedPresenter.this, "onFailure-->" + t.getMessage());
                onLoadedError();
            }
        });
    }

    @Override
    public void getContentById(int categoryId) {
        Call<SelectedContent> task = mApi.getSelectedContent(categoryId);
        task.enqueue(new Callback<SelectedContent>() {
            @Override
            public void onResponse(Call<SelectedContent> call, Response<SelectedContent> response) {
                int code = response.code();
                SelectedContent content = response.body();
                if (code == HTTP_OK) {
                    if (mViewCallback != null) {
                        mViewCallback.onSelectedContentLoaded(content);
                    }
                } else {
                    LogUtils.d(SelectedPresenter.this, "request code-->" + code);
                    onLoadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedContent> call, Throwable t) {
                LogUtils.d(SelectedPresenter.this, "onFailure-->" + t.getMessage());
                onLoadedError();
            }
        });
    }

    @Override
    public void reload() {
        this.getCategories();
    }

    private void onLoadedError() {
        if (mViewCallback != null) {
            mViewCallback.onError();
        }
    }

    @Override
    public void registerViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback = callback;
    }

    @Override
    public void unregisterViewCallback(ISelectedPageCallback callback) {
        if (mViewCallback != null)
            mViewCallback = null;
    }
}
